cmake_minimum_required(VERSION 3.16)

project(desktop VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS Quick Core Widgets Sql Network QuickControls2 WebSockets)

qt_standard_project_setup(REQUIRES 6.8)

# 查找 libuv
# 优先查找 third_party/libuv/x64-windows 目录（vcpkg 格式）
find_path(LIBUV_INCLUDE_DIR uv.h
    PATHS
        ${CMAKE_SOURCE_DIR}/third_party/libuv/x64-windows/include
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/x64-windows/include
        ${CMAKE_SOURCE_DIR}/third_party/libuv/include
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/include
        /usr/local/include
        /usr/include
        $ENV{LIBUV_DIR}/include
        $ENV{LIBUV_ROOT}/include
)

find_library(LIBUV_LIBRARY 
    NAMES uv libuv uv_a
    PATHS
        ${CMAKE_SOURCE_DIR}/third_party/libuv/x64-windows/lib
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/x64-windows/lib
        ${CMAKE_SOURCE_DIR}/third_party/libuv/x64-windows/debug/lib
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/x64-windows/debug/lib
        ${CMAKE_SOURCE_DIR}/third_party/libuv/lib
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/lib
        /usr/local/lib
        /usr/lib
        $ENV{LIBUV_DIR}/lib
        $ENV{LIBUV_ROOT}/lib
)

if(NOT LIBUV_INCLUDE_DIR OR NOT LIBUV_LIBRARY)
    message(FATAL_ERROR 
        "libuv not found!\n"
        "Please ensure libuv is installed in:\n"
        "  - ${CMAKE_SOURCE_DIR}/third_party/libuv/x64-windows\n"
        "  Or set LIBUV_DIR environment variable pointing to libuv installation directory."
    )
endif()

message(STATUS "Found libuv:")
message(STATUS "  Include dir: ${LIBUV_INCLUDE_DIR}")
message(STATUS "  Library: ${LIBUV_LIBRARY}")



qt_add_executable(HiAPP
    main.cpp
    # database_example.cpp
    files/file_system_model.cpp
    files/file_system_model.h
    files/kms_file_system_watcher.cpp
    files/kms_file_system_watcher.h
    files/runtime_file_system_watcher.cpp
    service/runtime_file_system_watcher.h
    service/system_icon_provider.cpp
    service/system_icon_provider.h
    service/icon_image_provider.cpp
    service/icon_image_provider.h
    database/database_manager.cpp
    database/disk_repository.cpp
    database/folder_repository.cpp
    database/file_repository.cpp
    database/command_repository.cpp
    service/backend_service.cpp
    service/backend_service.h
    service/mouse_position.cpp
    service/mouse_position.h
    service/web_socket_server.cpp
    service/web_socket_server.h
    command/command_executor.cpp
    command/open_url_file_command.cpp
    command/commit_file_to_url_command.cpp
    command/sync_disks_command.cpp
    command/sync_folders_command.cpp
    command/sync_root_folder_command.cpp
    command/sync_files_command.cpp
    command/get_notice_command.cpp
    command/mark_read_notice_command.cpp
    command/download_file_command.cpp
    command/get_kms_file_by_file_id_command.cpp
    command/base_command.cpp
    command/rollback_file_command.cpp
    command/delete_file_command.cpp
    command/rename_file_command.cpp
    command/rename_folder_command.cpp
    command/delete_folder_command.cpp
    command/commit_file_command.cpp
    command/login_check_in_command.cpp
    command/exchange_access_token_command.cpp
    command/access_token_timer.cpp
    service/system_tray_manager.cpp
    service/system_tray_manager.h
    HiAPP.rc
)


# 添加资源文件
qt_add_resources(HiAPP "icons"
    PREFIX "/"
    FILES
        icons/folder.png
        icons/folder_root.png
        icons/txt.png
        icons/doc.png
        icons/docx.png
        icons/pdf.png
        icons/jpg.png
        icons/jpeg.png
        icons/png.png
        icons/gif.png
        icons/bmp.png
        icons/mp3.png
        icons/wav.png
        icons/avi.png
        icons/zip.png
        icons/rar.png
        icons/exe.png
        icons/dll.png
        icons/notice.png
        icons/notice_active.png
        icons/disk.png
        icons/disk_active.png
        icons/contects.png
        icons/contects_active.png
        icons/workplace.png
        icons/workplace_active.png
        icons/mycompany.png
        icons/mycompany_active.png
        icons/avatar.png
        icons/status_synced.png
        icons/status_pending_download.png
        icons/status_local_modified.png
        icons/status_local_added.png
        icons/status_local_deleted.png
        icons/status_modified_conflict.png
        icons/status_server_delete_conflict.png
        icons/status_local_delete_conflict.png
        icons/logo.ico
        icons/close.png
        icons/login.png
        icons/logout.png
        icons/show.png
)

qt_add_qml_module(HiAPP
    URI desktop
    VERSION 1.0
    QML_FILES
        qml/Main.qml
        qml/Login.qml
        qml/Disk.qml
        qml/FileObjectItem.qml
        qml/Notice.qml
        qml/Contect.qml
        qml/WindowToolBar.qml
        qml/ServerHostSetting.qml
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(HiAPP PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.HiAPP
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_link_libraries(HiAPP
    PRIVATE Qt6::Quick Qt6::Core Qt6::Widgets Qt6::Sql Qt6::Network Qt6::QuickControls2 Qt6::WebSockets
)

# 链接 libuv
target_link_libraries(HiAPP PRIVATE ${LIBUV_LIBRARY})
target_include_directories(HiAPP PRIVATE ${LIBUV_INCLUDE_DIR})


# Windows API 支持
if(WIN32)
    target_link_libraries(HiAPP
        PRIVATE shell32 shlwapi comctl32
    )
endif()

include(GNUInstallDirs)
install(TARGETS HiAPP
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
